In Dart there’s an ability to use template literals instead of concatenation. Since their use is clearer and more concise, they are preferred.
Exceptions
Concatenation of string literals, without any variables, is allowed, both using +
and using adjacent strings. Those are typically used
for multiline strings.
Raw string literals are also an exception to this rule, since they don’t support interpolation.
var s1 =
'hello\n' +
'world'; // OK
var s2 =
'hello\n'
'world'; // OK
var s3 = r'hello\n' + s1; // OK
The multiline strings like s1
and s2
above can also be written as follows:
var s1Alternative = '''
hello
world'''; // OK
Noncompliant code example
void sayHello(name) {
print('hello ' + name + '!'); // Noncompliant
}
Compliant solution
void sayHello(name) {
print('hello $name!');
}